package com.chainyoung.component_tmtrade.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmtrade.bean.TradeCurrencyItem;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeCurrencyCurrentListContract;

import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@FragmentScope
public class TradeCurrencyCurrentListPresenter extends BasePresenter<TradeCurrencyCurrentListContract.Model, TradeCurrencyCurrentListContract.View> {


    @Inject
    public TradeCurrencyCurrentListPresenter(TradeCurrencyCurrentListContract.Model model, TradeCurrencyCurrentListContract.View rootView) {
        super(model, rootView);
    }


    public void cancelExchange(TreeMap<String, Object> map) {
        addSubscribe(mModel.cancelTrade(map)
                .compose(RxUtils.<Object>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<Object>(mView) {
                    @Override
                    public void onNext(Object object) {
                        mView.onCancelTradeSuccess(object);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    public void getExchangeList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getExchangeTradeList(map)
                .compose(RxUtils.<CommonList<TradeCurrencyItem>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<TradeCurrencyItem>>(mView) {
                    @Override
                    public void onNext(CommonList<TradeCurrencyItem> data) {
                        mView.onGetTradeListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException){
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)){
                                mView.onGetTradeListSuccess(null);
                                return;
                            }
                        }
                        mView.onGetTradeListFailed();
                        super.onError(e);
                    }
                }));
    }


}
